﻿using AutoMapper;
using Newtonsoft.Json;
using System.Collections.Generic;
using housingsafety.server.Models;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using System;
using housingsafety.server.Security;
using static housingsafety.server.Models.HelperModel;





namespace housingsafety.server
{
    /// <summary>
    /// 实体映射关系
    /// </summary>
    public class AutoMapperProfiles : Profile
    {
        /// <summary>
        /// 添加你的实体映射关系
        /// </summary>
        public AutoMapperProfiles()
        {
            //User转UserDto.
            CreateMap<ZmUser, UserDto>()
                .ForMember(nameof(UserDto.District), opt
                => opt.MapFrom(src => JsonConvert.DeserializeObject<SimpleDepartmentDto>(src.District)))
                //映射发生之前
                .BeforeMap((source, dto) =>
                {
                })
                //映射发生之后
                .AfterMap((source, dto) =>
                {
                    if (!string.IsNullOrEmpty(source.Encphone))
                    {
                        dto.Phone = AesHelper.Decrypt(source.Encphone);
                    }

                    if (source.RoleType.HasValue)
                    {
                        dto.Role = (PermissionRoleType)source.RoleType;
                    }
                    else
                    {
                        dto.Role = PermissionRoleType.Admin;
                    }

                    if (source.Group.HasValue)
                    {
                        dto.UserGroup = (UserGroupType)source.Group;
                    }
                    else
                    {
                        dto.UserGroup = UserGroupType.Unknown;
                    }

                    if (source.Jurisdiction != null)
                    {
                        dto.JurisdictionCodes = JsonConvert
                        .DeserializeObject<List<SimpleDepartmentDto>>(source.Jurisdiction);
                    }
                });
            CreateMap<UserDto, ZmUser>()
                //映射发生之前
                .BeforeMap((dto, source) =>
                {
                    //code ...
                })
                //映射发生之后
                .AfterMap((dto, source) =>
                {
                    source.Phone = $"{dto.Phone.Substring(0, 3)}****{dto.Phone.Substring(7, 4)}";
                    source.RoleType = Convert.ToInt16(JsonConvert.SerializeObject(dto.Role));
                });
            CreateMap<ZmSystemLog, SystemLogDto>()
                .ForMember(nameof(SystemLogDto.RespRet), opt
                => opt.MapFrom(src => JsonConvert.DeserializeObject<RespRet>(src.Respret)))
                ;
            CreateMap<ZmDepartment, DepartmentDto>();
            CreateMap<ZmBuildings, ZmBuildingsLayer>();
            CreateMap<ZmBuildings, BuildingAttrDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       //var centroid = source.Geom.Centroid;
                       //if (centroid != null)
                       //{
                       //    //dto.Lng = centroid.X;
                       //    //dto.Lat = centroid.Y;
                       //}
                   }
               });
            CreateMap<ZmBuildings, BuildingDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           //XMin = source.Geom.EnvelopeInternal.MinX,
                           //XMax = source.Geom.EnvelopeInternal.MaxX,
                           //YMin = source.Geom.EnvelopeInternal.MinY,
                           //YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }
               });
            CreateMap<ZmBuildingsLayer, BuildingGeomDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }

                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       //dto.BoundingBox = new ZmRect
                       //{
                       //    XMin = source.Geom.EnvelopeInternal.MinX,
                       //    XMax = source.Geom.EnvelopeInternal.MaxX,
                       //    YMin = source.Geom.EnvelopeInternal.MinY,
                       //    YMax = source.Geom.EnvelopeInternal.MaxY,
                       //};
                   }
               });
            CreateMap<ZmBuildingsLayer, BuildingAttrDto>()
              //映射发生之前
              .BeforeMap((source, dto) =>
              {

              })
              //映射发生之后
              .AfterMap((source, dto) =>
              {
                  if (source.Geom != null)
                  {
                      var centroid = source.Geom.Centroid;
                      if (centroid != null)
                      {
                          //dto.Lng = centroid.X;
                          //dto.Lat = centroid.Y;
                      }
                  }

                  //if (!dto.PcztFire.HasValue)
                  //{
                  //    dto.PcztFire = 0;
                  //}

                  //if (!dto.PcztHousing.HasValue)
                  //{
                  //    dto.PcztHousing = 0;
                  //}

                  //if (!dto.PcztPolice.HasValue)
                  //{
                  //    dto.PcztPolice = 0;
                  //}
              });
            CreateMap<ZmBuildingsLayer, BuildingDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }

                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           XMin = source.Geom.EnvelopeInternal.MinX,
                           XMax = source.Geom.EnvelopeInternal.MaxX,
                           YMin = source.Geom.EnvelopeInternal.MinY,
                           YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }

                   if (!dto.PcztFire.HasValue)
                   {
                       dto.PcztFire = 0;
                   }

                   if (!dto.PcztHousing.HasValue)
                   {
                       dto.PcztHousing = 0;
                   }

                   if (!dto.PcztPolice.HasValue)
                   {
                       dto.PcztPolice = 0;
                   }
               });
            CreateMap<ZmBuildingsLayer, BuildingGeomDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }

                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       //dto.BoundingBox = new ZmRect
                       //{
                       //    XMin = source.Geom.EnvelopeInternal.MinX,
                       //    XMax = source.Geom.EnvelopeInternal.MaxX,
                       //    YMin = source.Geom.EnvelopeInternal.MinY,
                       //    YMax = source.Geom.EnvelopeInternal.MaxY,
                       //};
                   }

               });
            CreateMap<ZmPlaceCommercial, PlaceBaseDto>();
            CreateMap<ZmPlaceCommercial, PlaceCommercialFireDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {
               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   dto.UseElectricBicyclePhotos = JsonStr2List(source.UseElectricBicyclePhoto);
                   dto.UseBottledLiquefiedGasPhotos = JsonStr2List(source.UseBottledLiquefiedGasPhoto);
                   dto.UseStackedItemsPhotos = JsonStr2List(source.UseStackedItemsPhoto);
                   dto.UseBurglarMeshPhotos = JsonStr2List(source.UseBurglarMeshPhoto);
                   dto.UseCeilingPhotos = JsonStr2List(source.UseCeilingPhoto);
                   dto.IsCompletelyIsolatedPhotos = JsonStr2List(source.IsCompletelyIsolatedPhoto);
                   dto.UseFlammableMaterialsPhotos = JsonStr2List(source.UseFlammableMaterialsPhoto);
                   dto.UnauthorizedElectricalConnectPhotos = JsonStr2List(source.UnauthorizedElectricalConnectPhoto);
                   dto.NotEnoughExitsPhotos = JsonStr2List(source.NotEnoughExitsPhoto);
                   dto.FireServiceDamagedPhotos = JsonStr2List(source.FireServiceDamagedPhoto);
                   dto.UseStoreDangerousGoodsPhotos = JsonStr2List(source.UseStoreDangerousGoodsPhoto);
                   dto.ThreatenSafetyFireHazardsPhotos = JsonStr2List(source.ThreatenSafetyFireHazardsPhoto);
                   dto.UseFlammableInsulationMaterialsPhotos = JsonStr2List(source.UseFlammableInsulationMaterialsPhoto);
                   dto.UseFlammableDangerousGoodsPhotos = JsonStr2List(source.UseFlammableDangerousGoodsPhoto);
                   dto.AboveThreeFloorSelfPhotos = JsonStr2List(source.AboveThreeFloorSelfPhoto);
                   dto.CheckPhotos = JsonStr2List(source.CheckPhoto);
               });
            CreateMap<ZmPlaceCommercial, PlaceCommercialIntegrationDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {
               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {

               });
            CreateMap<ZmPlaceCommercialCheck, PlaceCommercialFireCheckDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {
               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   dto.UseElectricBicyclePhotos = JsonStr2List(source.UseElectricBicyclePhoto);
                   dto.UseBottledLiquefiedGasPhotos = JsonStr2List(source.UseBottledLiquefiedGasPhoto);
                   dto.UseStackedItemsPhotos = JsonStr2List(source.UseStackedItemsPhoto);
                   dto.UseBurglarMeshPhotos = JsonStr2List(source.UseBurglarMeshPhoto);
                   dto.UseCeilingPhotos = JsonStr2List(source.UseCeilingPhoto);
                   dto.IsCompletelyIsolatedPhotos = JsonStr2List(source.IsCompletelyIsolatedPhoto);
                   dto.UseFlammableMaterialsPhotos = JsonStr2List(source.UseFlammableMaterialsPhoto);
                   dto.UnauthorizedElectricalConnectPhotos = JsonStr2List(source.UnauthorizedElectricalConnectPhoto);
                   dto.NotEnoughExitsPhotos = JsonStr2List(source.NotEnoughExitsPhoto);
                   dto.FireServiceDamagedPhotos = JsonStr2List(source.FireServiceDamagedPhoto);
                   dto.UseStoreDangerousGoodsPhotos = JsonStr2List(source.UseStoreDangerousGoodsPhoto);
                   dto.ThreatenSafetyFireHazardsPhotos = JsonStr2List(source.ThreatenSafetyFireHazardsPhoto);
                   dto.UseFlammableInsulationMaterialsPhotos = JsonStr2List(source.UseFlammableInsulationMaterialsPhoto);
                   dto.UseFlammableDangerousGoodsPhotos = JsonStr2List(source.UseFlammableDangerousGoodsPhoto);
                   dto.AboveThreeFloorSelfPhotos = JsonStr2List(source.AboveThreeFloorSelfPhoto);
                   dto.CheckPhotos = JsonStr2List(source.CheckPhoto);
                   dto.IsPcData = string.IsNullOrEmpty(source.CheckMind);
               });
            CreateMap<ZmPlaceResident, PlaceBaseDto>();
            CreateMap<ZmPlaceResident, PlaceResidentFireDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   dto.UseElectricBicyclePhotos = JsonStr2List(source.UseElectricBicyclePhoto);
                   dto.UseBottledLiquefiedGasPhotos = JsonStr2List(source.UseBottledLiquefiedGasPhoto);
                   dto.UseStackedItemsPhotos = JsonStr2List(source.UseStackedItemsPhoto);
                   dto.UseBurglarMeshPhotos = JsonStr2List(source.UseBurglarMeshPhoto);
                   dto.UseCeilingPhotos = JsonStr2List(source.UseCeilingPhoto);
                   dto.IsCompletelyIsolatedPhotos = JsonStr2List(source.IsCompletelyIsolatedPhoto);
                   dto.UseFlammableMaterialsPhotos = JsonStr2List(source.UseFlammableMaterialsPhoto);
                   dto.UnauthorizedElectricalConnectPhotos = JsonStr2List(source.UnauthorizedElectricalConnectPhoto);
                   dto.ChargingFacilitiesNoStandardPhotos = JsonStr2List(source.ChargingFacilitiesNoStandardPhoto);
                   dto.NotEnoughExitsPhotos = JsonStr2List(source.NotEnoughExitsPhoto);
                   dto.UseFlammableInsulationMaterialsPhotos = JsonStr2List(source.UseFlammableInsulationMaterialsPhoto);
                   dto.UseFlammableDangerousGoodsPhotos = JsonStr2List(source.UseFlammableDangerousGoodsPhoto);
                   dto.AboveThreeFloorSelfPhotos = JsonStr2List(source.AboveThreeFloorSelfPhoto);
                   dto.UseIndependentSmokeAlarmPhotos = JsonStr2List(source.UseIndependentSmokeAlarmPhoto);
                   dto.AboveThreeFloorsEscapePhotos = JsonStr2List(source.AboveThreeFloorsEscapePhoto);
                   dto.CheckPhotos = JsonStr2List(source.CheckPhoto);
               });
            CreateMap<ZmPlaceResident, PlaceResidentIntegrationDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {
               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {

               });
            CreateMap<ZmPlaceResidentCheck, PlaceResidentFireCheckDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   dto.UseElectricBicyclePhotos = JsonStr2List(source.UseElectricBicyclePhoto);
                   dto.UseBottledLiquefiedGasPhotos = JsonStr2List(source.UseBottledLiquefiedGasPhoto);
                   dto.UseStackedItemsPhotos = JsonStr2List(source.UseStackedItemsPhoto);
                   dto.UseBurglarMeshPhotos = JsonStr2List(source.UseBurglarMeshPhoto);
                   dto.UseCeilingPhotos = JsonStr2List(source.UseCeilingPhoto);
                   dto.IsCompletelyIsolatedPhotos = JsonStr2List(source.IsCompletelyIsolatedPhoto);
                   dto.UseFlammableMaterialsPhotos = JsonStr2List(source.UseFlammableMaterialsPhoto);
                   dto.UnauthorizedElectricalConnectPhotos = JsonStr2List(source.UnauthorizedElectricalConnectPhoto);
                   dto.ChargingFacilitiesNoStandardPhotos = JsonStr2List(source.ChargingFacilitiesNoStandardPhoto);
                   dto.NotEnoughExitsPhotos = JsonStr2List(source.NotEnoughExitsPhoto);
                   dto.UseFlammableInsulationMaterialsPhotos = JsonStr2List(source.UseFlammableInsulationMaterialsPhoto);
                   dto.UseFlammableDangerousGoodsPhotos = JsonStr2List(source.UseFlammableDangerousGoodsPhoto);
                   dto.AboveThreeFloorSelfPhotos = JsonStr2List(source.AboveThreeFloorSelfPhoto);
                   dto.UseIndependentSmokeAlarmPhotos = JsonStr2List(source.UseIndependentSmokeAlarmPhoto);
                   dto.AboveThreeFloorsEscapePhotos = JsonStr2List(source.AboveThreeFloorsEscapePhoto);
                   dto.CheckPhotos = JsonStr2List(source.CheckPhoto);
                   dto.IsPcData = string.IsNullOrEmpty(source.CheckMind);
               });
            CreateMap<ZmVillageFireSafety, VillageFireSafetyAttrDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }
                   }

                   dto.FireLayoutPhotos = JsonStr2List(source.FireLayoutPhoto);
                   dto.star_rating_reports = JsonStr2List(source.star_rating_report);
               });
            CreateMap<ZmVillageFireSafety, VillageFireSafetyDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           XMin = source.Geom.EnvelopeInternal.MinX,
                           XMax = source.Geom.EnvelopeInternal.MaxX,
                           YMin = source.Geom.EnvelopeInternal.MinY,
                           YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }

                   dto.FireLayoutPhotos = JsonStr2List(source.FireLayoutPhoto);
               });
            CreateMap<ZmVillageFireSafety, VillageFireSafetyGeomDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }

                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           XMin = source.Geom.EnvelopeInternal.MinX,
                           XMax = source.Geom.EnvelopeInternal.MaxX,
                           YMin = source.Geom.EnvelopeInternal.MinY,
                           YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }
               });
            CreateMap<ZmVillageFireSafetyYJ, VillageFireSafetyAttrDto>()
              //映射发生之前
              .BeforeMap((source, dto) =>
              {

              })
              //映射发生之后
              .AfterMap((source, dto) =>
              {
                  //if (source.Geom != null)
                  //{
                  //    var centroid = source.Geom.Centroid;
                  //    if (centroid != null)
                  //    {
                  //        dto.Lng = centroid.X;
                  //        dto.Lat = centroid.Y;
                  //    }
                  //}

                  //dto.FireLayoutPhotos = JsonStr2List(source.FireLayoutPhoto);
                  //dto.star_rating_reports = JsonStr2List(source.star_rating_report);
              });
            CreateMap<ZmVillageFireSafetyYJ, VillageFireSafetyDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           XMin = source.Geom.EnvelopeInternal.MinX,
                           XMax = source.Geom.EnvelopeInternal.MaxX,
                           YMin = source.Geom.EnvelopeInternal.MinY,
                           YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }

                   dto.FireLayoutPhotos = JsonStr2List(source.FireLayoutPhoto);
               });
            CreateMap<ZmVillageFireSafetyYJ, VillageFireSafetyGeomDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }

                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           XMin = source.Geom.EnvelopeInternal.MinX,
                           XMax = source.Geom.EnvelopeInternal.MaxX,
                           YMin = source.Geom.EnvelopeInternal.MinY,
                           YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }
               });


            CreateMap<ZmSurveyUnit, SurveyUnitDto>();
            CreateMap<ZmTask, SampleTaskDto>()
                //映射发生之前
                .BeforeMap((source, dto) =>
                {

                })
                //映射发生之后
                .AfterMap((source, dto) =>
                {
                    if (source.SampleAreaCode != null)
                    {
                        dto.SampleAreaCodes = JsonConvert
                        .DeserializeObject<List<SimpleDepartmentDto>>(source.SampleAreaCode);
                    }
                });
            CreateMap<ZmCityTask, SampleTaskDto>()
                //映射发生之前
                .BeforeMap((source, dto) =>
                {

                })
                //映射发生之后
                .AfterMap((source, dto) =>
                {
                    if (source.SampleAreaCode != null)
                    {
                        dto.SampleAreaCodes = JsonConvert
                        .DeserializeObject<List<SimpleDepartmentDto>>(source.SampleAreaCode);
                    }
                });
            CreateMap<RiskCensusHouseYhpc, AreaTaskBuildingExportDto>()
              //映射发生之前
              .BeforeMap((source, dto) =>
              {

              })
              //映射发生之后
              .AfterMap((source, dto) =>
              {
                  if (!string.IsNullOrEmpty(source.Aqyhcp))
                  {
                      if (source.Aqyhcp == "1")
                      {
                          dto.AqyhcpAlias = "存在严重安全隐患";
                      }
                      else if (source.Aqyhcp == "3")
                      {
                          dto.AqyhcpAlias = "存在一般安全隐患";
                      }
                      else if (source.Aqyhcp == "2")
                      {
                          dto.AqyhcpAlias = "未发现安全隐患";
                      }
                  }

                  if (source.Result.HasValue)
                  {
                      if (source.Result.Value == 1)
                      {
                          dto.ResultAlias = "正确";
                      }
                      else if (source.Result.Value == 0)
                      {
                          dto.ResultAlias = "不正确";
                      }
                  }
                  else
                  {
                      dto.ResultAlias = "未核查";
                  }
              });
            CreateMap<RiskCensusHouseYhpc, CityTaskBuildingExportDto>()
              //映射发生之前
              .BeforeMap((source, dto) =>
              {

              })
              //映射发生之后
              .AfterMap((source, dto) =>
              {
                  if (!string.IsNullOrEmpty(source.Aqyhcp))
                  {
                      if (source.Aqyhcp == "1")
                      {
                          dto.AqyhcpAlias = "存在严重安全隐患";
                      }
                      else if (source.Aqyhcp == "3")
                      {
                          dto.AqyhcpAlias = "存在一般安全隐患";
                      }
                      else if (source.Aqyhcp == "2")
                      {
                          dto.AqyhcpAlias = "未发现安全隐患";
                      }
                  }

                  if (source.Result.HasValue)
                  {
                      if (source.Result.Value == 1)
                      {
                          dto.ResultAlias = "正确";
                      }
                      else if (source.Result.Value == 0)
                      {
                          dto.ResultAlias = "不正确";
                      }
                  }
                  else
                  {
                      dto.ResultAlias = "未核查";
                  }

                  if (source.CityResult.HasValue)
                  {
                      if (source.CityResult.Value == 1)
                      {
                          dto.CityResultAlias = "正确";
                      }
                      else if (source.CityResult.Value == 0)
                      {
                          dto.CityResultAlias = "不正确";
                      }
                  }
                  else
                  {
                      dto.CityResultAlias = "未核查";
                  }
              });
            CreateMap<RiskCensusHouseYhpc, TaskBuildingAttrDto>()
              //映射发生之前
              .BeforeMap((source, dto) =>
              {

              })
              //映射发生之后
              .AfterMap((source, dto) =>
              {
                  if (source.Geom != null)
                  {
                      var centroid = source.Geom.Centroid;
                      if (centroid != null)
                      {
                          dto.Lng = centroid.X;
                          dto.Lat = centroid.Y;
                      }
                      var writer = new NetTopologySuite.IO.GeoJsonWriter();
                      dto.Geometry = writer.Write(source.Geom);
                  }

                  if (!string.IsNullOrEmpty(source.Aqyhcp))
                  {
                      if (source.Aqyhcp == "1")
                      {
                          dto.AqyhcpAlias = "存在严重安全隐患";
                      }
                      else if (source.Aqyhcp == "3")
                      {
                          dto.AqyhcpAlias = "存在一般安全隐患";
                      }
                      else if (source.Aqyhcp == "2")
                      {
                          dto.AqyhcpAlias = "未发现安全隐患";
                      }
                  }
              });
            CreateMap<RiskCensusHouseYhpc, TaskBuildingDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }

                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           XMin = source.Geom.EnvelopeInternal.MinX,
                           XMax = source.Geom.EnvelopeInternal.MaxX,
                           YMin = source.Geom.EnvelopeInternal.MinY,
                           YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }
               });
            CreateMap<RiskCensusHouseYhpc, TaskBuildingGeomDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Geom != null)
                   {
                       var centroid = source.Geom.Centroid;
                       if (centroid != null)
                       {
                           dto.Lng = centroid.X;
                           dto.Lat = centroid.Y;
                       }

                       var writer = new NetTopologySuite.IO.GeoJsonWriter();
                       dto.Geometry = writer.Write(source.Geom);
                       dto.BoundingBox = new ZmRect
                       {
                           XMin = source.Geom.EnvelopeInternal.MinX,
                           XMax = source.Geom.EnvelopeInternal.MaxX,
                           YMin = source.Geom.EnvelopeInternal.MinY,
                           YMax = source.Geom.EnvelopeInternal.MaxY,
                       };
                   }
               });
            CreateMap<ZmSampleCheck, SampleHouseCheckDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Btglx != null)
                   {
                       var a = source.Btglx.Split(",");
                       for (int i = 0; i < a.Length; i++)
                       {
                           switch (a[i])
                           {
                               case "1": dto.BtglxText += i + 1 + ".隐患等级判定错误"; break;
                               case "2": dto.BtglxText += i + 1 + ".管理措施不到位"; break;
                               case "3": dto.BtglxText += i + 1 + ".工程措施不到位"; break;
                               case "4": dto.BtglxText += i + 1 + ".其他问题"; break;
                               default: break;
                           }
                       }
                   }

                   dto.PhotoIds = JsonStr2List(source.Photos);
               });
            CreateMap<ZmCitySampleCheck, SampleHouseCheckDto>()
               //映射发生之前
               .BeforeMap((source, dto) =>
               {

               })
               //映射发生之后
               .AfterMap((source, dto) =>
               {
                   if (source.Btglx != null)
                   {
                       var a = source.Btglx.Split(",");
                       for (int i = 0; i < a.Length; i++)
                       {
                           switch (a[i])
                           {
                               case "1": dto.BtglxText += i + 1 + ".隐患等级判定错误"; break;
                               case "2": dto.BtglxText += i + 1 + ".管理措施不到位"; break;
                               case "3": dto.BtglxText += i + 1 + ".工程措施不到位"; break;
                               case "4": dto.BtglxText += i + 1 + ".其他问题"; break;
                               default: break;
                           }
                       }
                   }
                   dto.PhotoIds = JsonStr2List(source.Photos);
               });
            CreateMap<ZmYtFromDto, ZmYtFrom>()
               //映射发生之前
               .BeforeMap((source, po) =>
               {

               })
               //映射发生之后
               .AfterMap((source, po) =>
               {

               });
            CreateMap<ZmYtFrom, ZmYtFromDto>()
              //映射发生之前
              .BeforeMap((source, dto) =>
              {
                  if (dto.CreateOn == null)
                  {
                      source.CreateOn = DateTime.Now;
                  }
              })
              //映射发生之后
              .AfterMap((source, dto) =>
              {

              });
            CreateMap<ZmYtBuildDto, ZmYtBuild>()
             //映射发生之前
             .BeforeMap((source, dto) =>
             {

             })
             //映射发生之后
             .AfterMap((source, dto) =>
             {

             });
            CreateMap<ZmYtBuild, ZmYtBuildDto>()
            //映射发生之前
            .BeforeMap((source, dto) =>
            {

            })
            //映射发生之后
            .AfterMap((source, dto) =>
            {

            });
            CreateMap<ZmYtPhoto, ZmYtPhotoDto>()
           //映射发生之前
           .BeforeMap((source, dto) =>
           {

           })
           //映射发生之后
           .AfterMap((source, dto) =>
           {

           });
            CreateMap<ZmYtPhotoDto, ZmYtPhoto>()
            .BeforeMap((source, dto) =>
            {

            })
            //映射发生之后
            .AfterMap((source, dto) =>
            {

            });
            CreateMap<ZmYtFrom, ZmYtFrom>()
         .BeforeMap((source, dto) =>
         {

         })
         //映射发生之后
         .AfterMap((source, dto) =>
         {

         });
            CreateMap<ZmPlaceCommercialCheckLog, ZmPlaceCommercialCheckLogDto>()
         .BeforeMap((source, dto) =>
         {

         })
         //映射发生之后
         .AfterMap((source, dto) =>
         {

         });
            CreateMap<ZmPlaceCommercialCheckLogDto, ZmPlaceCommercialCheckLog>()
        .BeforeMap((source, dto) =>
        {

        })
        //映射发生之后
        .AfterMap((source, dto) =>
        {

        });
            CreateMap<ZmPlaceResidentCheckLog, ZmPlaceResidentCheckLogDto>()
      .BeforeMap((source, dto) =>
      {

      })
      //映射发生之后
      .AfterMap((source, dto) =>
      {

      });
            CreateMap<ZmPlaceResidentCheckLogDto, ZmPlaceResidentCheckLog>()
     .BeforeMap((source, dto) =>
     {

     })
     //映射发生之后
     .AfterMap((source, dto) =>
     {

     });
            CreateMap<ZmPcBaseLogCj, ZmPcBaseLogCjDto>()
           //映射发生之前
           .BeforeMap((source, dto) =>
           {

           })
           //映射发生之后
           .AfterMap((source, dto) =>
           {
               dto.Pcsj = source.Pcsj.HasValue ? source.Pcsj.Value.ToString("yyyy-MM-dd HH:mm:ss") :null;
               dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd HH:mm:ss");
               dto.ModifiedOn = source.ModifiedOn.HasValue ? source.ModifiedOn.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
           });

            CreateMap<ZmPcYhLogCj, ZmPcYhLogCjDto2>()
         //映射发生之前
         .BeforeMap((source, dto) =>
         {

         })
         //映射发生之后
         .AfterMap((source, dto) =>
         {
             dto.Pcsj = source.Pcsj.HasValue ? source.Pcsj.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
             dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd HH:mm:ss");
             dto.ModifiedOn = source.ModifiedOn.HasValue ? source.ModifiedOn.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
         });

            CreateMap<ZmPcYhLogSqjz, ZmPcYhLogCjDto2>()
       //映射发生之前
       .BeforeMap((source, dto) =>
       {

       })
       //映射发生之后
       .AfterMap((source, dto) =>
       {
           dto.Pcsj = source.Pcsj.HasValue ? source.Pcsj.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
           dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd HH:mm:ss");
           dto.ModifiedOn = source.ModifiedOn.HasValue ? source.ModifiedOn.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
       });

            CreateMap<ZmPcBaseLogSqjz, ZmPcBaseLogCjDto>()
      //映射发生之前
      .BeforeMap((source, dto) =>
      {

      })
      //映射发生之后
      .AfterMap((source, dto) =>
      {
          dto.Pcsj = source.Pcsj.HasValue ? source.Pcsj.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
          dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd HH:mm:ss");
          dto.ModifiedOn = source.ModifiedOn.HasValue ? source.ModifiedOn.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
      });
            CreateMap<ZmPcBaseLog, ZmPcBaseLogCjDto>()
  //映射发生之前
  .BeforeMap((source, dto) =>
  {

  })
  //映射发生之后
  .AfterMap((source, dto) =>
  {
      dto.Pcsj = source.Pcsj.HasValue ? source.Pcsj.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
      dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd HH:mm:ss");
      dto.ModifiedOn = source.ModifiedOn.HasValue ? source.ModifiedOn.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
  });

            CreateMap<ZmPcYhLog, ZmPcYhLogCjDto2>()
     //映射发生之前
     .BeforeMap((source, dto) =>
     {

     })
     //映射发生之后
     .AfterMap((source, dto) =>
     {
         dto.Pcsj = source.Pcsj.HasValue ? source.Pcsj.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
         dto.CreateOn = source.CreateOn.ToString("yyyy-MM-dd HH:mm:ss");
         dto.ModifiedOn = source.ModifiedOn.HasValue ? source.ModifiedOn.Value.ToString("yyyy-MM-dd HH:mm:ss") : null;
     });

        }


        private Dictionary<string, string> JsonStr2Dict(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
            }
            catch (System.Exception)
            {
            }

            return null;
        }

        private List<string> JsonStr2List(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<List<string>>(jsonStr);
            }
            catch (System.Exception)
            {
            }

            return null;
        }

        private ZmRect JsonStr2Bounds(string jsonStr)
        {
            try
            {
                var bounds = JsonConvert.DeserializeObject<double[]>(jsonStr);
                return new ZmRect
                {
                    XMin = bounds[0],
                    YMin = bounds[1],
                    XMax = bounds[2],
                    YMax = bounds[3]
                };
            }
            catch (System.Exception)
            {
            }

            return null;
        }
    }
}
